const Controller = require('egg').Controller

class CollectionController extends Controller {
  /* 后台 */
  async getCollections() {
    try {
      const { app, ctx } = this
      const { Op } = app.Sequelize
      const { searchForm, page, limit } = ctx.request.body
      let conditions = {}
      if(!searchForm) {
        conditions = {}
      } else {
        if(!!searchForm.title) {
          conditions.title = {[Op.startsWith]: searchForm.title}
        } 
        if(!!searchForm.public) {
          conditions.public = searchForm.public
        }
      }
      const collections = await ctx.service.collection.getList(conditions, page, limit)
      ctx.body = {
        code: '0',
        desc: '获取收藏夹成功',
        collections: collections,
        count: collections.count
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '获取收藏夹失败',
        err: error
      }
    }
  }

  async createCollection() {
    try {
      const { ctx } = this
      const { title, description, is_public, owner_id }= ctx.request.body
      await ctx.model.Collection.create({
        title,
        description,
        owner_id,
        public: is_public
      })
      ctx.body = {
        code: '0',
        desc: '收藏夹创建成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '收藏夹创建失败',
        err: error
      }
    }
  }

  async editCollection() {
    try {
      const { ctx } = this
      const { id, title, description, is_public, owner_id } = ctx.request.body
      await ctx.model.Collection.update({
        title,
        description,
        owner_id,
        public: is_public
      }, {
        where: {
          id: id
        }
      })
      ctx.body = {
        code: '0',
        desc: '收藏夹编辑成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '收藏夹编辑失败',
        err: error
      }
    }
  }

  async deleteCollection() {
    try {
      const { ctx } = this
      const { id } = ctx.request.body
      await ctx.model.Collection.destroy({
        where: {
          id: id
        }
      })
      ctx.body = {
        code: '0',
        desc: '收藏夹删除成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '收藏夹删除失败',
        err: error
      }
    }
  }

  async batchDeleteCollections() {
    try {
      const { app, ctx } = this
      const { Op } = app.Sequelize
      const { ids } = ctx.request.body
      await ctx.model.Collection.destroy({
        where: {
          id: {
            [Op.in]: ids
          }
        }
      })
      ctx.body = {
        code: '0',
        desc: '收藏夹批量删除成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '收藏夹批量删除失败'
      }
    }
  }

  async getCollectionArticles() {
    try {
      const { app, ctx } = this
      const { Op } = app.Sequelize
      const { collection_id, page, limit } = ctx.request.body
      const pageOffset = ctx.helper.toInt((page - 1) * limit);
      const pageLimit = ctx.helper.toInt(limit);
      const list = await ctx.model.ArticleCollection.findAll({
        where: {
          collection_id
        }
      })
      // 收集文章id
      let articleIds = []
      list.forEach(item => {
        articleIds.push(item.article_id)
      })
      const articles = await ctx.model.Article.findAndCountAll({
        include:[{
          model: app.model.User,
          attributes:['username','avatar']
        }],
        where: {
          id: {
            [Op.in]: articleIds
          }
        },
        limit: pageLimit,
        offset: pageOffset
      })
      ctx.body = {
        code: '0',
        desc: '收藏夹内容获取成功',
        articles: articles 
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '收藏夹内容获取失败',
        err: error
      }
    }
  }
}

module.exports = CollectionController